import 'package:flutter/material.dart';

import '../../../bean/envmonitoring_bean.dart';
import '../../../common/data_source.dart';
import '../../../common/system_config.dart';
import '../../../utils/tools/analysis_code.dart';
import '../../../utils/tools/screenutil_tool.dart';

class ListWidget extends StatelessWidget {
  List<Widget> lw = [];
  ListWidget(BuildContext context, List<EnvmonitoringBean> data) {
    lw = crateListWidget(context,data);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: lw,
    );
  }


  /// 遍历创建Widget
  /// #### [context] BuildContext|必填|上下文
  /// #### [data] List<WaterinVasionBean>|必填|响应的数据
  List<Widget> crateListWidget(BuildContext context, List<EnvmonitoringBean> data) {
    TextStyle textStyle = TextStyle(fontSize: SystemConfig.fs_title3);
    List<Widget> _insideListWidget = [];
    for(int i = 0;i < data.length; i++){
      _insideListWidget.add(
        Container(
          width: double.infinity,
          margin: EdgeInsets.only(top: ST.seth(20)),
          padding: EdgeInsets.symmetric(vertical: ST.seth(20),horizontal: ST.setw(20)),
          decoration: BoxDecoration(
            color: Theme.of(context).backgroundColor,
            borderRadius: BorderRadius.all(Radius.circular(8)),
            boxShadow: [
              BoxShadow(
                offset: Offset(0, 2), //x,y轴
                color: Color.fromRGBO(0, 0, 0, .1),
                blurRadius: 3 //投影距离
              )
            ]
          ),
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.only(bottom: ST.seth(20)),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("${data[i].sensorName}",style: textStyle),
                    Text("${data[i].createTime}",style: textStyle)
                  ],
                ),
              ),
              SizedBox(
                width: double.infinity,
                height: 1,
                child: Container(
                  color: SystemConfig.borderColor,
                ),
              ),
              stateWidget(data[i].status, data[i].temperature, data[i].isTemperatureAlarm, '温度'),
              SizedBox(
                width: double.infinity,
                height: 1,
                child: Container(
                  color: SystemConfig.borderColor,
                ),
              ),
              stateWidget(data[i].status, data[i].humidity, data[i].isHumidityAlarm, '湿度'),
            ],
          ),
        )
      );
    }
    return _insideListWidget;
  }


  /// 根据状态渲染widget
  /// #### [status] int|必填|设备状态
  /// #### [type] int|必填|对应的数值
  /// #### [name] String|必填|名称
  Widget stateWidget(int status, double type, int isAlarm, String name){
    TextStyle textStyle = TextStyle(fontSize: SystemConfig.fs_content3);
    Widget w;
    String Symbol = name == "温度" ? "℃" : "%";
    if(status == 1 || status == null || status == ""){
      w = Padding(
        padding: EdgeInsets.only(top: ST.seth(20),bottom: ST.seth(20)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text("$name",style: textStyle),
            Text("设备离线", style: TextStyle(
                color: Colors.red,
                fontSize: SystemConfig.fs_content3
              )
            )
          ],
        ),
      );
    } else {
      w = Padding(
        padding: EdgeInsets.only(top: ST.seth(20),bottom: ST.seth(20)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text("$name：$type $Symbol",style: textStyle),
            Text("${analysisCode(isAlarm,eEquipmentStateSourceData)["value"]}",
              style: TextStyle(
                color: isAlarm != 0 ? Colors.red : Colors.green,
                fontSize: SystemConfig.fs_content3
              )
            )
          ],
        ),
      );
    }
    return w;
  }


}